package com.thunbu.thunbu_meeting_system.viewMdel

import android.app.Activity
import android.app.ActivityManager
import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.hjq.toast.ToastUtils
import com.thunbu.lib_common.base.CoroutineViewModel
import com.thunbu.lib_common.utils.MFSPHelper
import com.thunbu.lib_common.utils.kick.KickInfo
import com.thunbu.lib_common.utils.kick.LogoutMonitor
import com.thunbu.lib_common.utils.okhttp.utils.L.e
import com.thunbu.thunbu_meeting_system.R
import com.thunbu.thunbu_meeting_system.await
import com.thunbu.thunbu_meeting_system.bean.AddressModel
import com.thunbu.thunbu_meeting_system.bean.HomeModel
import com.thunbu.thunbu_meeting_system.launch
import com.thunbu.thunbu_meeting_system.service.ApiService
import com.thunbu.thunbu_meeting_system.service.MainNet
import com.thunbu.thunbu_meeting_system.toRequestBody
import com.thunbu.thunbu_meeting_system.utils.AppUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import retrofit2.await
import java.util.concurrent.TimeUnit
import java.util.logging.Logger


/**
 * @Description:
 * @author: Yang.Yang33
 * @Date: 2023/3/16 21:26
 */
class MainViewModel : CoroutineViewModel<Unit>() {
    val homeInfo = MutableLiveData<HomeModel>()

    override fun initModel() {
        initLogoutMonitor()
    }

    private fun initLogoutMonitor() {
        val logoutSubscribe: Disposable = LogoutMonitor.getInstance().dataMonitor
                .observeOn(AndroidSchedulers.mainThread())
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe({ kickInfo ->
                    if (kickInfo == null) {
                        return@subscribe
                    }
                    val reason: KickInfo.REASON = kickInfo.getReason()
                    if (reason === KickInfo.REASON.COOKIE_INVALID) {
                        ToastUtils.show("用户信息失效，请重新登录")
                        AppUtils.loginOut()
                    }
                }) { throwable -> Log.e("loginout","用户信息失效，请重新登录",throwable) }
    }

    fun requestHomeInfo() {
        launch({
            val result = MainNet.server(ApiService::class.java).getHomeInfo().await(true,true)
            homeInfo.value = result.data
        })
    }

    /**
     * @Description: 请求资产分类,并缓存
     * @author: Yang.Yang33
     * @Date: 2023/4/6 15:42
     */
    fun requestCategoryGroup(onResume: Boolean) {
        if(onResume && AppUtils.mAssetType != null) return
        launch({
            val result = MainNet.server(ApiService::class.java).getCategoryGroup( mutableMapOf<String, Any?>().toRequestBody()).await(needErrorToast = false)
            val data = result.data
            withContext(Dispatchers.IO){
                data.categoryName = data.categoryName?:"全部"
                AppUtils.setAssetType(data)
            }
        })
    }

    /**
     * @Description: 请求存放地址列表
     * @author: Yang.Yang33
     * @Date: 2023/4/10 13:17
     */
    fun requestAddress(onResume: Boolean) {
        if(onResume && AppUtils.mAddressInfo != null) return
        launch({
            val result = MainNet.server(ApiService::class.java).get_place_list().await(needErrorToast = false)
            val data = result.data
            withContext(Dispatchers.IO){
                AppUtils.setAddressInfo(data as ArrayList<AddressModel>?)
            }
        })
    }

    /**
     * @Description: 请求部门所有组织
     * @author: Yang.Yang33
     * @Date: 2023/4/10 13:17
     */
    fun requestDepartment(onResume: Boolean) {
        if(onResume && AppUtils.mDepartmentInfo != null) return
        launch({
            val result = MainNet.server(ApiService::class.java).getTenantAllDept( ).await(needErrorToast = false)
            val data = result.data
            withContext(Dispatchers.IO){
                AppUtils.setDepartmentInfo(data)
            }
        })
    }
    /**
     * @Description: 请求部门组织人员
     * @author: Yang.Yang33
     * @Date: 2023/4/10 13:17
     */
    fun requestDepartUser(onResume: Boolean) {
        if(onResume && AppUtils.mDepartmentUserInfo != null) return
        launch({
            val result = MainNet.server(ApiService::class.java).getTenantAllDeptUser( ).await(needErrorToast = false)
            val data = result.data
            withContext(Dispatchers.IO){
                AppUtils.setDepartmentUserInfo(data)
            }
        })
    }

    fun requestData(onResume: Boolean) {
        requestHomeInfo()
        requestCategoryGroup(onResume)
        requestAddress(onResume)
        requestDepartment(onResume)
        requestDepartUser(onResume)
    }
}